﻿Imports System.Data.SqlClient

Public Class frmAppraisalFormArchive

    Private Sub frmAppraisalFormArchive_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
    End Sub

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Try
            Me.Cursor = Cursors.Default

            Dim cmd As New SqlCommand("Select Distinct MoveNo,EmpID,EmpName " & _
                                      "From StaffAppraisalForm " & _
                                      "Where CreatedOn>=N'" & Me.DTPFrom.Value.ToShortDateString & " 00:00:00' " & _
                                      "and   CreatedOn<=N'" & Me.DTPTo.Value.ToShortDateString & " 23:59:59'", cnn)
            Dim Reader As SqlDataReader

            Me.ListVouchers.Items.Clear()
            Me.CrystalReportViewer1.ReportSource = Nothing

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Dim Item As New ListViewItem
                Item.Text = Reader.Item("MoveNo")
                Item.SubItems.Add(Reader.Item("EmpID"))
                Item.SubItems.Add(Reader.Item("EmpName"))

                Me.ListVouchers.Items.Add(Item)
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub ListVouchers_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListVouchers.SelectedIndexChanged
        Me.CrystalReportViewer1.ReportSource = Nothing

        If Me.ListVouchers.SelectedItems.Count > 0 Then
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim dap As New SqlDataAdapter("Select * From StaffAppraisalForm Where MoveNo=" & CInt(Me.ListVouchers.SelectedItems(0).Text) , cnn)
                Dim das As New DataSet

                dap.Fill(das, "StaffAppraisalForm")

                Dim rpt As New StaffAppraisalForm
                rpt.SetDataSource(das)
                Me.CrystalReportViewer1.ReportSource = rpt

                Me.ListVouchers.Focus()

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub
End Class